import { HttpException, HttpStatus, Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { In, Repository } from 'typeorm';
import { Borrow } from './borrow.entity';
import { CreateDto } from './dto/create.dto'
import { RemoveDto } from './dto/remove.dto'
import { UpdateDto } from './dto/update.dto'
import { GetBorrowsDto, borrowResponse } from './dto/get-borrow.dto'

@Injectable()
export class BorrowService {
  constructor(
    @InjectRepository(Borrow)
    private borrowRepository: Repository<Borrow>,
  ) { }

  async createBorrow(createDto: CreateDto): Promise<{ isCreated: boolean, message: string, code: number }> {

    const newBorrow = new Borrow();
    newBorrow.userId = createDto.userId;
    newBorrow.borrowDate = createDto.borrowDate;
    newBorrow.amount = createDto.amount;
    newBorrow.borrowType = createDto.borrowType;
    newBorrow.attach = createDto.attach;
    newBorrow.note = createDto.note;
    newBorrow.lender = createDto.lender;

    await this.borrowRepository.save(newBorrow);



    return { isCreated: true, message: "添加借出成功!", code: 200 };
  }

  async getBorrowsByUserId(getBorrowsDto: GetBorrowsDto): Promise<{ data: Array<borrowResponse>, message: string, code: number }> {
    if (!getBorrowsDto.userId) {
      throw new HttpException('用户id不能为空!', HttpStatus.BAD_REQUEST);
    }
    const query = await this.borrowRepository.findAndCount({ where: { userId: getBorrowsDto.userId, status: "1" } });
    let message = query[1] ? "查询成功!" : "根据用户id,查询结果为空!";
    const data = query[0].map(v => ({
      dataId: v.dataId,
      userId: v.userId,
      borrowDate: v.borrowDate,
      createdDate: v.createdDate,
      amount: v.amount,
      borrowType: v.borrowType,
      attach: v.attach,
      note: v.note,
      lender: v.lender
    }));

    return { data, message, code: 200 };
  }

  async removeBorrowsByDataIds(removeDto: RemoveDto): Promise<{ isRemoved: boolean, message: string, code: number }> {
    if (!removeDto.dataIds?.length) {
      throw new HttpException('dataIds不能为空!', HttpStatus.BAD_REQUEST);
    }
    const removed = await this.borrowRepository.update({ dataId: In(removeDto.dataIds),status:"1" }, { status: "0" });
    if (removed.affected && removed.affected > 0) {
      return { isRemoved: true, message: removed.affected + "条记录, 删除成功!", code: 200 };
    } else {
      return { isRemoved: false, message: "删除失败!", code: 400 };
    }

  }

  async updateBorrowByDataId(updateDto: UpdateDto): Promise<{ isUpdated: boolean, message: string, code: number }> {
    if (!updateDto.dataId) {
      throw new HttpException('dataId不能为空!', HttpStatus.BAD_REQUEST);
    }
    const updated = await this.borrowRepository.update({ dataId: updateDto.dataId,status:"1" }, {
      borrowDate: updateDto.borrowDate,
      amount: updateDto.amount,
      borrowType: updateDto.borrowType,
      attach: updateDto.attach,
      note: updateDto.note
    });
    if (updated.affected && updated.affected > 0) {
      return { isUpdated: true, message: "修改成功!", code: 200 };
    } else {
      return { isUpdated: false, message: "修改失败!", code: 400 };
    }

  }
}